﻿using CleanArchitecture.Core.SharedKernel;
using System;
using System.Collections.Generic;
using System.Linq;

namespace CleanArchitecture.Infrastructure.Entities.AdminUsers
{
    /// <summary>
    /// Represents a setting
    /// DavidLee 2019/11/23 EF Core在数据库实体类映射多表关系时使用lazyload模式加载数据时
    /// 要求映射属性要有对应的backing field方便数据对应
    /// 记录此坑注意事项：https://docs.microsoft.com/en-us/ef/core/modeling/backing-field
    ///                http://www.mamicode.com/info-detail-2330030.html
    ///  属性：AdminuserAdminroleMappings 要有对应的backing field（即 _adminuserAdminroleMappings）               
    ///  命名要符合驼峰规范: 
    ///    _<camel-cased property name>
    ///    _<property name>
    ///    m_<camel-cased property name>
    ///    m_<property name>
    /// 否则Ef core 报： No field was found backing property '{property}' of entity type '{entity}'. Either name the backing field so that it is picked up by convention
    /// DavidLee 2020/02/20 Abandoned LazyLoading For unexpected Null Reference Exception
    /// </summary>
    public partial class AdminUser : BaseEntity
    {
        private ICollection<AdminUserAdminRoleMapping> _adminuserAdminroleMappings;
        //private IList<AdminRole> _adminRoles;
        /// <summary>
        /// Ctor
        /// </summary>
        public AdminUser()
        {   
        }

        /// <summary>
        /// 用户登录名 支持字母/数字/中文组合 但需要校验唯一性
        /// </summary>
        public string UserLoginName { get; set; }

        /// <summary>
        /// 用户登录手机号 需要校验唯一性
        /// </summary>
        public string Mobile { get; set; }

        /// <summary>
        /// 用户登录邮箱号 需要校验唯一性
        /// </summary>
        public string Email { get; set; }

        /// <summary>
        /// 用户登录系统显示名称
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 用户是否处于锁定状态，如登录三次密码校验错误将会处于锁定状态 1-未锁定 0-锁定
        /// </summary>
        public EntityEnumConfig.LockState IsActive { get; set; }

        /// <summary>
        /// 用户是否已注销 1-注销 0-正常使用
        /// </summary>
        public EntityEnumConfig.DeleteState IsDeleted { get; set; }

        public DateTime LastLoginTimeUtc { get; set; }
        public string LastLoginIp { get; set; }
        public DateTime CreatedOnTimeUtc { get; set; }

        /// <summary>
        /// 用户尝试登录次数
        /// </summary>
        public int FailedLoginAttempts { get; set; }

        //DavidLee 2019/11/18改造通过BaseEntity.ExtraInfo赋予对象信息
        /// <summary>
        /// Gets adminuser roles
        /// </summary>
        //public virtual IList<AdminRole> AdminRoles
        //{
        //    get => _adminRoles ?? (_adminRoles = AdminuserAdminroleMappings.Select(mapping => mapping.AdminRole).ToList());
        //}

        /// <summary>
        /// Gets or sets adminuser-adminrole mappings
        /// </summary>
        public virtual ICollection<AdminUserAdminRoleMapping> AdminuserAdminroleMappings
        {
            get => _adminuserAdminroleMappings ?? (_adminuserAdminroleMappings = new List<AdminUserAdminRoleMapping>());
            protected set => _adminuserAdminroleMappings = value;
        }
        #region Methods

        /// <summary>
        /// Add adminuser adminrole and reset adminroles cache
        /// </summary>
        /// <param name="role">Role</param>
        //public void AddAdminuserAdminroleMapping(AdminUserAdminRoleMapping role)
        //{
        //    AdminuserAdminroleMappings.Add(role);
        //    _adminRoles = null;
        //}

        /// <summary>
        /// Remove adminuser adminrole and reset adminroles cache
        /// </summary>
        /// <param name="role">Role</param>
        //public void RemoveAdminuserAdminroleMapping(AdminUserAdminRoleMapping role)
        //{
        //    AdminuserAdminroleMappings.Remove(role);
        //    _adminRoles = null;
        //}

        #endregion
    }
}